//
//  RightDetailTool.m
//  Daybook
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "RightDetailTool.h"
#import "Common.h"

@implementation RightDetailTool

+ (RightDetailTool *)shareRightDetailTool {
    static RightDetailTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[RightDetailTool alloc] init];
    });
    return tool;
}

- (NSMutableArray *)arrayFromModel:(ChartModel *)model beginDate:(NSDate *)beginDate endDate:(NSDate *)endDate {
    NSMutableArray *resultArr = [NSMutableArray array];
    NSString *beginStr = [[CalendarTool shareCalendarTool] getMonthAndDayStringFromDate:beginDate];
    NSString *endStr = [[CalendarTool shareCalendarTool] getMonthAndDayStringFromDate:endDate];
    
    NSInteger endMonth = [[CalendarTool shareCalendarTool] month:endDate];
    
    // 判断是否是一个月内
    if ([[beginStr substringToIndex:2] isEqualToString:[endStr substringToIndex:2]]) {
        // 在一个月之内
        NSInteger tempBegin = [[CalendarTool shareCalendarTool] day:beginDate];
        NSInteger tempEnd = [[CalendarTool shareCalendarTool] day:endDate];

        for (NSInteger i = tempEnd; i >= tempBegin; i--) {
            // 每一天的数据
            NSMutableString *dayStr = [NSMutableString stringWithFormat:@"%04ld-%02ld-%02ld", [[CalendarTool shareCalendarTool] year:endDate], [[CalendarTool shareCalendarTool] month:endDate], i];
            NSDate *dayDate = [[CalendarTool shareCalendarTool] dateFromString:dayStr];
            NSDate *dayBeginDate = [[CalendarTool shareCalendarTool] beginDateFromSmallStringDate:dayDate];
            NSDate *dayEndDate = [[CalendarTool shareCalendarTool] endDateFromSmallStringDate:dayDate];

            RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
            DayBook *currentDayBook = [result firstObject];
            
            RLMResults *accountBooks = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and name = %@ and type = %@", dayBeginDate, dayEndDate, model.name, model.type];

            if (accountBooks.count != 0) {
                NSMutableDictionary *dayDic = [NSMutableDictionary dictionary];
                [resultArr addObject:dayDic];
                double count = 0;
                [dayStr deleteCharactersInRange:NSMakeRange(0, 5)];
                [dayStr replaceCharactersInRange:NSMakeRange(2, 1) withString:@"月"];
                [dayStr appendString:@"日"];
//                NSLog(@"daayaa: %@", dayStr);
                [dayDic setValue:dayStr forKey:@"day"];
                NSMutableArray *dayCellArr = [NSMutableArray array];
                [dayDic setValue:dayCellArr forKey:@"dayCellArr"];
                for (NSInteger i = accountBooks.count - 1; i >= 0; i--) {
                    NSMutableDictionary *eachData = [NSMutableDictionary dictionary];
                    Account *account = accountBooks[i];
                    [eachData setValue:account.name forKey:@"name"];
                    [eachData setValue:account.picName forKey:@"picName"];
                    [eachData setValue:account.type forKey:@"type"];
                    [eachData setValue:account.time forKey:@"time"];
                    [eachData setValue:account.remarks forKey:@"remarks"];
                    if ([account.type isEqualToString:@"1"]) {
                        [eachData setValue:[NSString stringWithFormat:@"%.2f", account.income.doubleValue] forKey:@"money"];
                        count += account.income.doubleValue;
                    }else {
                        [eachData setValue:[NSString stringWithFormat:@"%.2f", account.money.doubleValue] forKey:@"money"];
                        count += account.money.doubleValue;
                    }
                    [dayCellArr addObject:eachData];
                }
                [dayDic setValue:[NSString stringWithFormat:@"%.2f", count] forKey:@"amount"];
            }
        }
    }else {
       // 没有在一个月之内
//        if ([[CalendarTool shareCalendarTool] month:beginDate] < [[CalendarTool shareCalendarTool] month:endDate]) {
//           // 在一年以内
            NSInteger tempBegin = [[CalendarTool shareCalendarTool] day:beginDate];
            NSInteger tempEnd = [[CalendarTool shareCalendarTool] day:endDate];
            NSInteger totalDays = [[CalendarTool shareCalendarTool] totaldaysInMonth:beginDate];

            for (NSInteger i = totalDays; i >= tempBegin; i--) {
                // 每一天的数据
                NSMutableString *dayStr = [NSMutableString stringWithFormat:@"%04ld-%02ld-%02ld", [[CalendarTool shareCalendarTool] year:endDate], [[CalendarTool shareCalendarTool] month:endDate], i];
                
               
                NSDate *dayDate = [[CalendarTool shareCalendarTool] dateFromString:dayStr];
                NSDate *dayBeginDate = [[CalendarTool shareCalendarTool] beginDateFromSmallStringDate:dayDate];
                NSDate *dayEndDate = [[CalendarTool shareCalendarTool] endDateFromSmallStringDate:dayDate];
                
                RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
                DayBook *currentDayBook = [result firstObject];
                
                RLMResults *accountBooks = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and name = %@ and type = %@", dayBeginDate, dayEndDate, model.name, model.type];
                
//                RLMRealm *realm = [RLMRealm defaultRealm];
//                RLMResults<AccountBook *> *accountBooks = [AccountBook objectsInRealm:realm where:@"time >  %@ and time < %@ and name = %@ and type = %@", dayBeginDate, dayEndDate, model.name, model.type];
                if (accountBooks.count != 0) {
                    NSMutableDictionary *dayDic = [NSMutableDictionary dictionary];
                    [resultArr addObject:dayDic];
                    double count = 0;
                    [dayStr deleteCharactersInRange:NSMakeRange(0, 5)];
                    [dayStr replaceCharactersInRange:NSMakeRange(2, 1) withString:@"月"];
                    [dayStr appendString:@"日"];
                    NSLog(@"daayaa: %@", dayStr);
                    [dayDic setValue:dayStr forKey:@"day"];
                    NSMutableArray *dayCellArr = [NSMutableArray array];
                    [dayDic setValue:dayCellArr forKey:@"dayCellArr"];
                    for (NSInteger i = accountBooks.count - 1; i >= 0; i--) {
                        NSMutableDictionary *eachData = [NSMutableDictionary dictionary];
                        Account *account = accountBooks[i];
                        [eachData setValue:account.name forKey:@"name"];
                        [eachData setValue:account.picName forKey:@"picName"];
                        [eachData setValue:account.type forKey:@"type"];
                        [eachData setValue:account.time forKey:@"time"];
                        [eachData setValue:account.remarks forKey:@"remarks"];
                        if ([account.type isEqualToString:@"1"]) {
                            [eachData setValue:[NSString stringWithFormat:@"%.2f", account.income.doubleValue] forKey:@"money"];
                            count += account.income.doubleValue;
                        }else {
                            [eachData setValue:[NSString stringWithFormat:@"%.2f", account.money.doubleValue] forKey:@"money"];
                            count += account.money.doubleValue;
                        }

                        [dayCellArr addObject:eachData];
                    }
                    [dayDic setValue:[NSString stringWithFormat:@"%.2f", count] forKey:@"amount"];
                }
            }
            for (NSInteger i = tempEnd; i >= 1; i--) {
                // 每一天的数据
                NSMutableString *dayStr = [NSMutableString stringWithFormat:@"%04ld-%02ld-%02ld", [[CalendarTool shareCalendarTool] year:endDate], [[CalendarTool shareCalendarTool] month:endDate], i];
                
                NSDate *dayDate = [[CalendarTool shareCalendarTool] dateFromString:dayStr];
                NSDate *dayBeginDate = [[CalendarTool shareCalendarTool] beginDateFromSmallStringDate:dayDate];
                NSDate *dayEndDate = [[CalendarTool shareCalendarTool] endDateFromSmallStringDate:dayDate];
                
                RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
                DayBook *currentDayBook = [result firstObject];
                
                RLMResults *accountBooks = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@ and name = %@ and type = %@", dayBeginDate, dayEndDate, model.name, model.type];

                if (accountBooks.count != 0) {
                    NSMutableDictionary *dayDic = [NSMutableDictionary dictionary];
                    [resultArr addObject:dayDic];
                    double count = 0;
                    [dayStr deleteCharactersInRange:NSMakeRange(0, 5)];
                    [dayStr replaceCharactersInRange:NSMakeRange(2, 1) withString:@"月"];
                    [dayStr appendString:@"日"];
//                    NSLog(@"daayaa: %@", dayStr);
                    [dayDic setValue:dayStr forKey:@"day"];
                    NSMutableArray *dayCellArr = [NSMutableArray array];
                    [dayDic setValue:dayCellArr forKey:@"dayCellArr"];
                    for (NSInteger i = accountBooks.count - 1; i >= 0; i--) {
                        NSMutableDictionary *eachData = [NSMutableDictionary dictionary];
                        Account *account = accountBooks[i];
                        [eachData setValue:account.name forKey:@"name"];
                        [eachData setValue:account.picName forKey:@"picName"];
                        [eachData setValue:account.type forKey:@"type"];
                        [eachData setValue:account.time forKey:@"time"];
                        
                        [eachData setValue:account.remarks forKey:@"remarks"];
                        if ([account.type isEqualToString:@"1"]) {
                            [eachData setValue:[NSString stringWithFormat:@"%.2f", account.income.doubleValue] forKey:@"money"];
                            count += account.income.doubleValue;
                        }else {
                            [eachData setValue:[NSString stringWithFormat:@"%.2f", account.money.doubleValue] forKey:@"money"];
                            count += account.money.doubleValue;
                        }
                        [dayCellArr addObject:eachData];
                    }
                    [dayDic setValue:[NSString stringWithFormat:@"%.2f", count] forKey:@"amount"];
                }
            }
//        }else {
//           // 在一年以外
// 
//        }
    }


    return resultArr;
}

- (NSMutableArray *)modelArrayFromModel:(ChartModel *)model beginDate:(NSDate *)beginDate endDate:(NSDate *)endDate {
    NSMutableArray *tempArr = [self arrayFromModel:model beginDate:beginDate endDate:endDate];
    return [RightDetailModel baseModalByArr:tempArr];
}

@end
